package com.hwhhhh.mygrad.utils;

import android.app.DatePickerDialog;
import android.content.Context;
import android.os.Build;
import android.widget.DatePicker;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class MyDatePickerDialog extends DatePickerDialog {

    @RequiresApi(api = Build.VERSION_CODES.N)
    public MyDatePickerDialog(@NonNull Context context) {
        super(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public MyDatePickerDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public MyDatePickerDialog(@NonNull Context context, @Nullable OnDateSetListener listener, int year, int month, int dayOfMonth) {
        super(context, listener, year, month, dayOfMonth);
        setLimitedTime(year, month, dayOfMonth, 7);
    }

    public MyDatePickerDialog(@NonNull Context context, int themeResId, @Nullable OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) {
        super(context, themeResId, listener, year, monthOfYear, dayOfMonth);
    }

    // 限制在limitedDay天内
    private void setLimitedTime(int year, int month, int day, int limitedDay) {
        DatePicker datePicker = this.getDatePicker();
        GregorianCalendar gregorianCalendar = new GregorianCalendar(year, month, day + limitedDay);
        datePicker.setMinDate(Calendar.getInstance().getTimeInMillis());
        datePicker.setMaxDate(gregorianCalendar.getTimeInMillis());
    }
}
